<?php


namespace app\admin\controller;

use think\facade\Request;
use app\common\service\VerifyService;
use app\admin\validate\AdminVerifyValidate;
use app\admin\validate\AdminAdminValidate;
use app\admin\service\AdminLoginService;
use app\admin\service\AdminSettingService;

class AdminLogin
{
    /**
     * @api {get}  admin/AdminLogin/verify 验证码
     * @apiVersion 0.1.0
     * @apiName verify
     * @apiGroup 后台登录接口
     *
     *
     * @apiSuccess {String} firstname Firstname of the User.
     * @apiSuccess {String} lastname  Lastname of the User.
     * @apiSampleRequest admin/AdminLogin/verify
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "code": 200,
     *       "msg": "操作成功",
     *       "data": {
     *       "verify_switch": true,
     *       "verify_id": "507bd1f88b9dc72ff9756e8a6cfb2013",
     *       "verify_src": ""
     *            }
     *       }
     *
     * @apiError UserNotFound The id of the User was not found.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function verify()
    {
        $config = AdminSettingService::settingVerify();

        $data =  VerifyService::create($config);

        return success($data);
    }

    /**
     * @api {get}  admin/AdminLogin/login 登录
     * @apiVersion 0.1.0
     * @apiName login
     * @apiGroup 后台登录接口
     *
     * @apiParam {string} username 用户名称.
     * @apiParam {string} password 密码.
     * @apiHeader {string} AdminToken 返回的token.
     * @apiHeader {string} AdminAdminId 用户admin_admin_id .
     *
     * @apiSuccess {String} admin_admin_id 用户id.
     * @apiSuccess {String} admin_token  用户token.
     * @apiSampleRequest admin/AdminLogin/login
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *           code: 200,
     *           msg: "登录成功",
     *           data: {
     *           admin_admin_id: 2,
     *           admin_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJsampBZG1pbiIsImlhdCI6MTYyOTE2Nzg2OCwibmJmIjoxNjI5MTY3ODY4LCJleHAiOjE2MjkyMTEwNjgsImRhdGEiOnsiYWRtaW5fYWRtaW5faWQiOjIsImxvZ2luX3RpbWUiOiIyMDIxLTA4LTE3IDEwOjM3OjQ4IiwibG9naW5faXAiOiIxMjcuMC4wLjEifX0.QhnW5Y-3QiQ1tJBkpIHkhIcBr6JplHaI_byvZxN9k8E"
     *               }
     *           }
     *
     * @apiError UserNotFound The id of the User was not found.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "UserNotFound"
     *     }
     */
    public function login()
    {
        $param['username']    = Request::param('username/s', '');
        $param['password']    = Request::param('password/s', '');
        $param['verify_id']   = Request::param('verify_id/s', '');
        $param['verify_code'] = Request::param('verify_code/s', '');

        $config = AdminSettingService::settingVerify();
        
        if ($config['switch']) {
            validate(AdminVerifyValidate::class)->scene('check')->check($param);
        }

        validate(AdminAdminValidate::class)->scene('admin_login')->check($param);

        $data = AdminLoginService::login($param);

        return success($data, '登录成功');
    }

    /**
     * 退出
     *
     * @method POST
     * 
     * @return json
     */
    public function logout()
    {
        $param['admin_admin_id'] = admin_admin_id();

        validate(AdminAdminValidate::class)->scene('admin_id')->check($param);

        $data = AdminLoginService::logout($param['admin_admin_id']);

        return success($data, '退出成功');
    }
}
